package com.alibaba.middleware.hsf.guide.serial.hessian.bitset;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.BitSet;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import com.taobao.hsf.com.caucho.hessian.io.AbstractHessianInput;
import com.taobao.hsf.com.caucho.hessian.io.AbstractHessianOutput;
import com.taobao.hsf.com.caucho.hessian.io.Hessian2Input;
import com.taobao.hsf.com.caucho.hessian.io.Hessian2Output;


/**
 * <pre>
 * 验证Hessian不支持java.util.BitSet
 * 
 * </pre>
 * 
 * @author weipeng2k 2015年10月8日 下午4:56:43
 */
public class HessianSerial {
    
    static File file;
    
    @BeforeClass
    public static void createFile() {
        try {
            file = File.createTempFile("hessianserial", "dat");
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    @Test
    public void serial() throws Exception {
        BitSet bs = new BitSet();
        for (int i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                bs.set(i);
            }
        }
        System.out.println(bs);
        AbstractHessianOutput out = new Hessian2Output(new FileOutputStream(file));
        out.writeObject(bs);
        out.flush();
    }
    
    @Test
    public void deserial() throws Exception {
        AbstractHessianInput in = new Hessian2Input(new FileInputStream(file));
        BitSet cl = (BitSet) in.readObject();
        Assert.assertTrue(cl.length() == 0);
    }
    
    @AfterClass
    public static void removeFile() {
        file.delete();
    }
}
